// ConsoleBase060.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

//int main(int argc, char* argv[])
//{
//	printf("Hello World!\n");
//	return 0;
//}

// crt_exec.c
// Illustrates the different versions of exec, including
//      _execl          _execle          _execlp          _execlpe
//      _execv          _execve          _execvp          _execvpe
//
// Although CRT_EXEC.C can exec any program, you can verify how 
// different versions handle arguments and environment by 
// compiling and specifying the sample program CRT_ARGS.C. See 
// "_spawn, _wspawn Functions" for examples of the similar spawn 
// functions.


#include <stdio.h>
#include <conio.h>
#include <process.h>

// add
#include <stdlib.h>  //solve the error C2065: atoi : undeclared identifier

char *my_env[] =     // Environment for exec?e
{
   "THIS=environment will be",
   "PASSED=to new process by",
   "the EXEC=functions",
   NULL
};

int main( int ac, char* av[] )
{
   char *args[4];
   //int ch;
   if( ac != 3 ){
      fprintf( stderr, "Usage: %s <program> <number (1-8)>\n", av[0] );
      return 1;
   }

   // Arguments for _execv? 
   args[0] = av[1];
   args[1] = "exec??";
   args[2] = "two";
   args[3] = NULL;

/*
e: envp, array of pointers to environment settings, is passed to the new process.
l: Command-line arguments are passed individually to _exec function. 
   Typically used when the number of parameters to the new process is known in advance.
p: PATH environment variable is used to find the file to execute.
v: argv, array of pointers to command-line arguments, is passed to _exec. 
   Typically used when the number of parameters to the new process is variable.
*/
   switch( atoi( av[2] ) )
   {
   case 1:
      _execl( av[1], av[1], "_execl", "two", NULL );
      break;
   case 2:
      _execle( av[1], av[1], "_execle", "two", NULL, my_env );
      break;
   case 3:
      _execlp( av[1], av[1], "_execlp", "two", NULL );
      break;
   case 4:
      _execlpe( av[1], av[1], "_execlpe", "two", NULL, my_env );
      break;
   case 5:
      _execv( av[1], args );
      break;
   case 6:
      _execve( av[1], args, my_env );
      break;
   case 7:
      _execvp( av[1], args );
      break;
   case 8:
      _execvpe( av[1], args, my_env );
      break;
   default:
      break;
   }

   // This point is reached only if exec fails. 
   printf( "\nProcess was not execed." );
   exit( 0 );
}

